Swift の Codable (JSONEncoder or JSONDecoder) で空オブジェクトを扱う

struct EmptyResponse: Codable {}

みたいなのを用意すれば OK

import Foundation

struct EmptyResponse: Codable {}

struct Response: Codable {
    let foo: EmptyResponse?
    let bar: EmptyResponse?
}

let data = try! JSONEncoder().encode(Response(foo: nil, bar: nil))
let data1 = try! JSONEncoder().encode(Response(foo: .init(), bar: nil))

dump(String(data: data, encoding: .utf8)!) // - "{}"
dump(String(data: data1, encoding: .utf8)!) // - "{\"foo\":{}}"

Swift Codable JSONEncoder JSONDecoder